package aor2.cpu.blockregisters;

import aor2.components.InstructionCode;
import aor2.components.ElementIO;
import aor2.components.Signal;
import aor2.cpu.blockcontrol.ControlSignalSet;

public class GPR implements ElementIO {

	private static GPR singleton = null;

	public static GPR getInstance() {
		if (singleton == null)
			singleton = new GPR(ControlSignalSet.getInstance().get("opsel"));
		return singleton;
	}

	private Signal opsel;

	public void in() {
		long ir0 = InstructionRegister.IR0.getState();
		int reg;
		if (opsel.isActive()) {
			reg = (int) (ir0 & InstructionCode.reg1mask) >> 6;
		} else {
			reg = (int) (ir0 & InstructionCode.reg2mask);
		}
		switch (reg) {
		case 0:
			GPRegisters.R0.in();
			break;
		case 1:
			GPRegisters.R1.in();
			break;
		case 2:
			GPRegisters.R2.in();
			break;
		case 3:
			GPRegisters.R3.in();
			break;
		case 4:
			GPRegisters.R4.in();
			break;
		case 5:
			GPRegisters.R5.in();
			break;
		case 6:
			GPRegisters.R6.in();
			break;
		case 7:
			GPRegisters.R7.in();
			break;
		}
	}

	public void out() {
		long ir0 = InstructionRegister.IR0.getState();
		int reg;
		if (opsel.isActive()) {
			reg = (int) (ir0 & InstructionCode.reg1mask) >> 6;
		} else {
			reg = (int) (ir0 & InstructionCode.reg2mask);
		}
		switch (reg) {
		case 0:
			GPRegisters.R0.out();
			break;
		case 1:
			GPRegisters.R1.out();
			break;
		case 2:
			GPRegisters.R2.out();
			break;
		case 3:
			GPRegisters.R3.out();
			break;
		case 4:
			GPRegisters.R4.out();
			break;
		case 5:
			GPRegisters.R5.out();
			break;
		case 6:
			GPRegisters.R6.out();
			break;
		case 7:
			GPRegisters.R7.out();
			break;
		}
	}

	private GPR(Signal opsel) {
		super();
		this.opsel = opsel;
		ControlSignalSet css = ControlSignalSet.getInstance();
		css.get("REGin").addDestinationElementIO(this);
		css.get("REGout").addSourceElementIO(this);
		TempRegisters.A.getState();// samo da se instancira
	}

}
